package fc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class FrontController
 */
@WebServlet("/FrontController")
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession sess = request.getSession();
		// Ueberprueft ob User eingeloggt ist.
		if(sess.getAttribute("eingeloggt") == null){
			sess.setAttribute("eingeloggt", "false");
		}
		
		RequestDispatcher rd;
			if(request.getRequestURI().contains("login") || (!request.getRequestURI().contains("login")&sess.getAttribute("eingeloggt").equals("true"))){
				rd = getServletContext().getNamedDispatcher("jsp");
				rd.forward(request, response);
				return;
			}
			// Ist User eingeloggt wird auf Main-Page weitergeleitet.
			if(sess.getAttribute("eingeloggt").equals("true")){
				rd = getServletContext().getRequestDispatcher("/Main.jsp");

			}
			else{
			// Ist User nicht eingeloggt wird auf Login-Page verwiesen.	
				rd = getServletContext().getRequestDispatcher("/login.jsp");
			}
		rd.forward(request, response);
		
	}

}
